package com.kelvin.paopao.module.main.date.datemain;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kelvin.kelvinlib.utils.LogUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.DateViewPagerAdapter;
import com.kelvin.paopao.module.base.BaseFragment;
import com.kelvin.paopao.module.main.date.datelist.MainDateListFragment;
import com.kelvin.paopao.utils.AnimateHelper;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.utils.LoginUtil;
import com.kelvin.paopao.widgets.dialog.PublishDateFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/6.
 */

public class MainDateFragment extends BaseFragment implements ViewPager.OnPageChangeListener, TabLayout.OnTabSelectedListener, DialogInterface.OnDismissListener {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    DateViewPagerAdapter adapter;

    private View tabView;

    public static final int TYPE_DATE_ALL = 0;
    public static final int TYPE_DATE_SING = 2;
    public static final int TYPE_DATE_EAT = 0;
    public static final int TYPE_DATE_MOVIES = 3;
    public static final int TYPE_DATE_TRAVEL = 1005;
    public static final int TYPE_DATE_SPORT = 1;
    public static final int TYPE_DATE_OTHER = 4;

    private MainDateListFragment allFragment, singFragment, movieFragment,
            eatFragment, travelFragment, sportFragment,
            otherFragment;

    private List<Fragment> fragmentList;

    private int[] tabResId = {R.drawable.selector_main_date_list_all, R.drawable.selector_main_date_list_sing,
            R.drawable.selector_main_date_list_eat, R.drawable.selector_main_date_list_movie,
            R.drawable.selector_main_date_list_travel, R.drawable.selector_main_date_list_sport,
            R.drawable.selector_main_date_list_other};

    private String[] tabTv = {"全部", "唱歌", "电影", "吃饭", "旅游", "运动", "其他"};

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_date;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        initTab();
        initMainDateListFragment();

        adapter = new DateViewPagerAdapter(getChildFragmentManager());
        adapter.setItem(fragmentList);
        viewPager.setAdapter(adapter);

        //这个方法会清除全部的tab
//        tabLayout.setupWithViewPager(viewPager);

        viewPager.addOnPageChangeListener(this);
        tabLayout.setOnTabSelectedListener(this);
    }

    /**
     * 初始化邀約的全部fragment
     */
    private void initMainDateListFragment() {
        allFragment = MainDateListFragment.newInstance(TYPE_DATE_ALL);
        singFragment = MainDateListFragment.newInstance(TYPE_DATE_SING);
        movieFragment = MainDateListFragment.newInstance(TYPE_DATE_MOVIES);
        eatFragment = MainDateListFragment.newInstance(TYPE_DATE_EAT);
        travelFragment = MainDateListFragment.newInstance(TYPE_DATE_TRAVEL);
        sportFragment = MainDateListFragment.newInstance(TYPE_DATE_SPORT);
        otherFragment = MainDateListFragment.newInstance(TYPE_DATE_OTHER);

        fragmentList = new ArrayList<>();
        fragmentList.add(allFragment);
        fragmentList.add(singFragment);
        fragmentList.add(movieFragment);
        fragmentList.add(eatFragment);
        fragmentList.add(travelFragment);
        fragmentList.add(sportFragment);
        fragmentList.add(otherFragment);
    }

    /**
     * 初始化tablayout的tab
     */
    private void initTab() {
        for (int i = 0; i < 7; i++) {
            TabLayout.Tab tab = tabLayout.newTab();

            tabView = LayoutInflater.from(getContext())
                    .inflate(R.layout.tab_main_date_list_item, null);
            TextView tv = (TextView) tabView.findViewById(R.id.tv_tab);
            ImageView iv = (ImageView) tabView.findViewById(R.id.iv_tab);
            tv.setText(tabTv[i]);
            iv.setImageResource(tabResId[i]);
            tab.setCustomView(tabView);

            tabLayout.addTab(tab);
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tabLayout.getTabAt(position).select();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @OnClick(R.id.fab)
    public void onClick() {
        if (Constant.IS_LOGIN) {
            AnimateHelper.fabOpen(fab);
            PublishDateFragment publishDateFragment = PublishDateFragment.newInstance();
            FragmentManager manager = getChildFragmentManager();
            publishDateFragment.show(manager, "PublishDateFragment");
        } else {
            LoginUtil.checkLogin(mContext);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        AnimateHelper.fabClose(fab);
    }
}
